package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.sumpu.app.coolsk.dao.TripDao;
import cn.sumpu.app.coolsk.domain.Trip;
import cn.sumpu.app.coolsk.domain.TripDay;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class TripDaoImpl extends SqlSessionDaoSupport implements TripDao{
	
	private static final Logger logger = LoggerFactory.getLogger(MemberDaoImpl.class);
	
	//行程
	@Override
	public void insterTrip(Trip trip)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.TripMapper.insterTrip",trip);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insterTrip database failure.", e);
			}
			throw new AppBusinessException("insterTrip database failure.", e);
		}
	}
	//行程时间内容
	@Override
	public void insterTripDay(TripDay tripDay)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.TripMapper.insterTripDay",tripDay);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insterTripDay database failure.", e);
			}
			throw new AppBusinessException("insterTripDay database failure.", e);
		}
	}
	//行程总数
	@Override
	public int getTripCountByMid(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.TripMapper.getTripCountByMid",map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTripCountByMid database failure.", e);
			}
			throw new AppBusinessException("getTripCountByMid database failure.", e);
		}
	}
	//
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Trip> getTripListByMid(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.TripMapper.getTripListByMid",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTripListByMid database failure.", e);
			}
			throw new AppBusinessException("getTripListByMid database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<TripDay> selectTripDayListBytid(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.TripMapper.selectTripDayListBytid",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectTripDayListBytid database failure.", e);
			}
			throw new AppBusinessException("selectTripDayListBytid database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<TripDay> selectDayListBytid(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.TripMapper.selectDayListBytid",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectDayListBytid database failure.", e);
			}
			throw new AppBusinessException("selectDayListBytid database failure.", e);
		}
	}
	
	//行程管理
	@SuppressWarnings("unchecked")
	@Override
	public List<Trip> getTripList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.TripMapper.getTripList",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTripList database failure.", e);
			}
			throw new AppBusinessException("getTripList database failure.", e);
		}
	}
	@Override
	public int getTripCount(Map<String, Object> map) throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.TripMapper.getTripCount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTripCount database failure.", e);
			}
			throw new AppBusinessException("getTripCount database failure.", e);
		}
	}
	
	@Override
	public Trip selectTripById(String id) throws AppBusinessException{
		
		try {
			return (Trip) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.TripMapper.selectTripById",id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectTripById failure.", e);
			}
			throw new AppBusinessException("selectTripById failure.", e);
		}
	}
	@Override
	public void updateTrip(Trip trip)throws AppBusinessException {
		
		try { 
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.TripMapper.updateTrip",trip);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateTrip database failure.", e);
			}
			throw new AppBusinessException("updateTrip database failure.", e);
		}
	}
	@Override
	public void deleteAllDay(Map<String, Object> map)throws AppBusinessException {
		
		try { 
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.TripMapper.deleteAllDay",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteAllDay database failure.", e);
			}
			throw new AppBusinessException("deleteAllDay database failure.", e);
		}
	}
	
}
